package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Override
    public PageResponse<NursingProjectVo> pageNursingProject(String name, Integer pageNum, Integer pageSize, Integer status) {
        //分页插件
        PageHelper.startPage(pageNum, pageSize);
        //查询数据库 返回数据
        Page<NursingProject> nursingProjectList = nursingProjectMapper.pageNursingProject(name, status);
        //转换为vo
        PageResponse<NursingProjectVo> nursingProjectVoPageResponse = PageResponse.of(nursingProjectList, NursingProjectVo.class);
        return nursingProjectVoPageResponse;
    }

    @Override
    public void addNursingProject(NursingProjectDto nursingProjectDto) {
        //换转为实体类
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        //添加时间
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProject.setCreateTime(LocalDateTime.now());
        //创建人，后续修改为正在操作的用户id
        // TODO
        nursingProject.setCreateBy(1L);
        nursingProject.setUpdateBy(1L);
        nursingProject.setStatus(0);
        nursingProjectMapper.addNursingProject(nursingProject);
    }

    @Override
    public NursingProjectVo getNursingProjectById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.seleteById(id);
        NursingProjectVo nursingProjectVo = BeanUtil.toBean(nursingProject, NursingProjectVo.class);
        return nursingProjectVo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateNursingProject(NursingProjectDto nursingProjectDto) {
        //转换实体类
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProject.setUpdateBy(1L);
        //根据id查询关联表
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanMapper.seleteById(nursingProject.getId());
        //判断是否与护理计划有关联
        if (!ObjectUtil.isEmpty(nursingProjectPlans)) {
            //不为空
            throw new RuntimeException("该项目已经与计划绑定，不允许修改");
        }
        //为空
        nursingProjectMapper.updateNursingProject(nursingProject);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        //根据id查询关联表
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanMapper.seleteById(id);
        if (!ObjectUtil.isEmpty(nursingProjectPlans)) {
            //不为空
            throw new RuntimeException("该项目已经与计划绑定，不允许删除");
        }
        nursingProjectMapper.updataStatus(id, status);
    }

    @Override
    public void deleteNursingProject(Long id) {
        //根据id查询关联表
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanMapper.seleteById(id);
        if (!ObjectUtil.isEmpty(nursingProjectPlans)) {
            //不为空
            throw new RuntimeException("该项目已经与计划绑定，不允许禁用");
        }
        nursingProjectMapper.deleteNursingProject(id);
    }

    @Override
    public List<NursingProjectVo> getNursingProjectList() {
        List<NursingProject> nursingProjectList = nursingProjectMapper.seleteList();
        List<NursingProjectVo> nursingProjectVos = BeanUtil.copyToList(nursingProjectList, NursingProjectVo.class);
        return nursingProjectVos;
    }
}
